package com.example.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.util.Log;

import com.example.constant.AppConstant;
import com.example.constant.VariableKey;

public class HttpUtilWithParameter {

    public static String getCookMethod(String url, String cb) {
        try {
            HttpPost post = new HttpPost(url);
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            NameValuePair pair1 = new BasicNameValuePair("cb", cb);
            pairs.add(pair1);
            HttpEntity entity = new UrlEncodedFormEntity(pairs, "UTF-8");// 设置编码，防止中文乱码
            post.setEntity(entity);
            HttpResponse httpResponse = new DefaultHttpClient().execute(post);
            String result = EntityUtils.toString(httpResponse.getEntity());
            return result;
        } catch (Exception e) {
            Log.e("bad", e.getMessage());
        }
        return null;
    }

    public static boolean ValidUser(String url, String username, String password) {
        try {
            HttpPost post = new HttpPost(url);
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            NameValuePair pair1 = new BasicNameValuePair("username", username);
            NameValuePair pair2 = new BasicNameValuePair("password", password);
            pairs.add(pair1);
            pairs.add(pair2);
            HttpEntity entity = new UrlEncodedFormEntity(pairs, "UTF-8");// 设置编码，防止中文乱码
            post.setEntity(entity);
            HttpResponse httpResponse = new DefaultHttpClient().execute(post);
            String result = EntityUtils.toString(httpResponse.getEntity());
            if ((null != result) && ("true".equalsIgnoreCase(result))) {
                AppConstant.GLOBAL_VARIABLE.put(VariableKey.USER_ID, username);
                return true;
            }

        } catch (Exception e) {
            Log.e("bad", e.getMessage());
        }
        return false;
    }

    public static String Register(String url, String username, String password) {
        String result = "0";
        try {
            HttpPost post = new HttpPost(url);
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            NameValuePair pair1 = new BasicNameValuePair("username", username);
            NameValuePair pair2 = new BasicNameValuePair("password", password);
            pairs.add(pair1);
            pairs.add(pair2);
            HttpEntity entity = new UrlEncodedFormEntity(pairs, "UTF-8");// 设置编码，防止中文乱码
            post.setEntity(entity);
            HttpResponse httpResponse = new DefaultHttpClient().execute(post);
            result = EntityUtils.toString(httpResponse.getEntity());
            System.out.println("result:" + result);

        } catch (Exception e) {
            Log.e("bad", e.getMessage());
        }
        return result;
    }

    public static boolean collecte(String url, String userid, String cbid) {
        boolean result = false;
        try {
            HttpPost post = new HttpPost(url);
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            NameValuePair pair1 = new BasicNameValuePair("userID", userid);
            NameValuePair pair2 = new BasicNameValuePair("cbID", cbid);
            pairs.add(pair1);
            pairs.add(pair2);
            HttpEntity entity = new UrlEncodedFormEntity(pairs, "UTF-8");// 设置编码，防止中文乱码
            post.setEntity(entity);
            HttpResponse httpResponse = new DefaultHttpClient().execute(post);
            String response = EntityUtils.toString(httpResponse.getEntity());
            if ((null != response) && ("true".equalsIgnoreCase(response))) {
                result = true;
            }

        } catch (Exception e) {
            Log.e("bad", e.getMessage());
        }
        return result;
    }
    
    public static String Getcollection(String url, String id) {
        String result = "0";
        try {
            HttpPost post = new HttpPost(url);
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            NameValuePair pair1 = new BasicNameValuePair("userID", id);
            pairs.add(pair1);
            HttpEntity entity = new UrlEncodedFormEntity(pairs, "UTF-8");// 设置编码，防止中文乱码
            post.setEntity(entity);
            HttpResponse httpResponse = new DefaultHttpClient().execute(post);
            result = EntityUtils.toString(httpResponse.getEntity());
            System.out.println("result:" + result);

        } catch (Exception e) {
            Log.e("bad", e.getMessage());
        }
        return result;
    }
}
